package gzdev;

import com.sun.xml.internal.fastinfoset.tools.FI_DOM_Or_XML_DOM_SAX_SAXEvent;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取配置文件中指定资源
 */
public class GzConfig {

    // 运行模式，
    private final String MODE = "mode";

    public static final int MODE_FORMAL = 1;
    public static final int MODE_ADD = 2;
    public static final int MODE_FORCE = 3;
    public static final int MODE_CONFIG = 4;

    private final String FILE_NAME = "/gzGeneratorMybatisCode.properties";

    // 数据源驱动名称
    public static final String DRIVER_NAME = "driverName";


    // 数据源url
    public static final String URL = "url";

    // 数据源的用户名
    public static final String USERNAME = "username";

    // 数据源的密码
    public static final String PASSWORD = "password";

    private static final String POJO_PACKAGE = "package.pojo";

    private static final String DAO_PACKAGE = "package.dao";

    // 配置数据库表的前缀
    private final String TABLE = "table";

    // 配置dao接口类名称后缀
    private final String SUFFIX_DAO = "suffix.dao";

    // 数据库的数据类型配置前缀
    private final String SQL_DATA_TYPE_PREFIX = "sql.";

    private Properties properties;

    // 获取运行模式，默认值 1
    public int getMode(){
        return Integer.parseInt(properties.getProperty(MODE, "1"));
    }

    // 获取实体类文件包名, 默认值 gzdev.pojo
    public String getPojoPackage(){
        return properties.getProperty(POJO_PACKAGE, "gzdev.pojo");
    }

    // 获取dao接口类的后缀
    public String getDaoSuffix(){
        return properties.getProperty(SUFFIX_DAO, "Dao");
    }

    // 获取dao接口文件包名，默认值 gzdev.dao
    public String getDaoPackage(){
        return properties.getProperty(DAO_PACKAGE, "gzdev.dao");
    }

    // 从配置文件中获取数据库表名
    public Set<String> getTableNames() throws IOException {
        String names = properties.getProperty(TABLE);
        if (names == null)
            throw new IOException("请配置需要代码生成的表名, 例如： table=tb1,tb2 ");
        return Arrays.stream(names.split(",")).collect(Collectors.toSet());
    }

    public GzConfig() throws IOException {
        properties = new Properties();
        InputStream inputStream = this.getClass().getResourceAsStream(FILE_NAME);
        if (inputStream == null)
            throw new IOException(String.format("配置文件 %s 没找到",FILE_NAME));
        properties.load(inputStream);
        inputStream.close();
    }

    public Map<String,String> getDatasourceConfig(){
        Map<String, String> params = new HashMap<>();
        params.put(DRIVER_NAME, properties.getProperty(DRIVER_NAME, "com.mysql.cj.jdbc.Driver"));
        params.put(USERNAME, Objects.requireNonNull(properties.getProperty(USERNAME), "数据源的用户名不能为空"));
        params.put(PASSWORD, properties.getProperty(PASSWORD));
        params.put(URL, Objects.requireNonNull(properties.getProperty(URL), "数据源的URL不能为空"));
        return params;
    }

    /**
     * 将数据库的数据类型转化成java类型
     * @param sqlType
     * @return
     */
    public String getJavaType(String sqlType){
        return properties.getProperty(SQL_DATA_TYPE_PREFIX + sqlType, "String");
    }

}
